//
//  LanguageView.swift
//  koolcar
//
//  Created by xiantian on 2023/9/18.
//

import UIKit

typealias LanguageViewBlock = (NSInteger) -> Void
class LanguageView: UIView {
    var callBack: LanguageViewBlock?
    var collectionView: UICollectionView!
    var selectIndex: NSInteger = 0
    var dataArray: [String] = [
        MMLocalizedString("launch_hk", comment: "繁體中文"),
        MMLocalizedString("launch_cn", comment: "简体中文"),
        MMLocalizedString("launch_en", comment: "English")
    ]
    override init(frame: CGRect) {
        super.init(frame: frame)
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 10
        layout.itemSize = CGSize.init(width: (kScreenWidth - 48)/3, height: 46)
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 46), collectionViewLayout: layout)
        self.collectionView.register(LanguageCell.self, forCellWithReuseIdentifier: "LanguageCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 0)
        self.collectionView.backgroundColor = UIColor.white
        self.addSubview(self.collectionView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LanguageView: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LanguageCell", for: indexPath) as! LanguageCell
        if self.dataArray.count > indexPath.row {
            cell.titleLab.text = self.dataArray[indexPath.row]
        }
        cell.setLanguageCell(select: indexPath.row == self.selectIndex)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        self.callBack?(indexPath.row)
        self.collectionView.reloadData()
    }
}



class LanguageCell: UICollectionViewCell {
    var titleLab: UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLab = UILabel()
        titleLab.textColor = Theme.titleLabColor
        titleLab.backgroundColor = Theme.whiteColor
        titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 20)
        titleLab.numberOfLines = 1
        titleLab.lineBreakMode = .byTruncatingTail
        titleLab.textAlignment = .center
        titleLab.layer.cornerRadius = 8
        titleLab.layer.masksToBounds = true
        titleLab.layer.borderWidth = 1
        titleLab.layer.borderColor = Theme.tfBorderColor.cgColor
        contentView.addSubview(titleLab)
        
        titleLab.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    func setLanguageCell(select: Bool) {
        if select {
            titleLab.textColor = Theme.whiteColor
            titleLab.backgroundColor = Theme.mainColor
        }else{
            titleLab.textColor = Theme.titleLabColor
            titleLab.backgroundColor = Theme.whiteColor
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
